// webpack plugins
var webpackMerge = require('webpack-merge');
var commonConfig = require('./webpack.config.common.js');
var path = require("path");
var CompressionPlugin = require("compression-webpack-plugin");
var UglifyJSPlugin = require('uglifyjs-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CompressionPlugin = require("compression-webpack-plugin");
var webpack = require("webpack");

/**
 * config
 * Reference: http://webpack.github.io/docs/configuration.html
 */
module.exports = webpackMerge(commonConfig, {
  module: {
    rules: [{
        test: /\.ts$/,
        // loaders: ['ts-loader', 'angular2-template-loader'],
        include: path.resolve(__dirname, './src'),
        exclude: /node_modules|aot/g,
        use: [{
            loader: "awesome-typescript-loader",
            options: {
              // configFileName: "tsconfig.prod.json",
              transpileOnly: true,
              useCache: true
            }
          },
          'angular2-template-loader',
          {
            loader: 'string-replace-loader',
            options: {
              multiple: [{
                search: 'test\.58caimi\.com',
                replace: 'hrhg.integrity.com.cn',
                flags: 'ig'
              }, {
                search: 'www\.58caimi\.com',
                replace: 'hrhg.integrity.com.cn',
                flags: 'ig'
              }, {
                search: 'ogvk6eo7a\.bkt\.clouddn\.com',
                replace: 'images.integrity.com.cn',
                flags: 'ig'
              }]
            }
          }
          // "@ngtools/webpack"
        ],
        enforce: "pre"
      },
      {
        test: /\.json$/,
        include: path.resolve(__dirname, './src'),
        use: [
          "json-loader",
          {
            loader: 'string-replace-loader',
            options: {
              multiple: [{
                search: 'test\.58caimi\.com',
                replace: 'hrhg.integrity.com.cn',
                flags: 'ig'
              }, {
                search: 'www\.58caimi\.com',
                replace: 'hrhg.integrity.com.cn',
                flags: 'ig'
              }, {
                search: 'ogvk6eo7a\.bkt\.clouddn\.com',
                replace: 'images.integrity.com.cn',
                flags: 'ig'
              }]
            },
          }
        ],
        enforce: "pre"
      },
    ]
  },
  devtool: 'source-map',
  devServer: {
    inline: true,
    hot: true,
    historyApiFallback: true
  },
  output: {
    path: path.resolve(__dirname, 'build'),
    publicPath: '/',
    filename: 'scripts/[name].[hash:7].js',
    chunkFilename: 'scripts/[id].[hash:7].chunk.js'
  },
  plugins: [
    new ExtractTextPlugin('styles/[name].[hash:7].css'),
    new CompressionPlugin({
      asset: "[path].gz[query]",
      algorithm: "gzip",
      test: /\.(js|html)$/,
      threshold: 10240,
      minRatio: 0.8
    }),
    new webpack.DefinePlugin({
      BUILD_IN_PRODUCTION: JSON.stringify(true)
    }),
    new UglifyJSPlugin({
      comments: false
    })
  ]
});